var info = "";

app.controller('txDataCtrl', ['$scope', function($scope) {
	var needNewPagination = true;
	getData(1);
	
	function getData(pageNo) {
		var state = $("#report-state").val();
		var userKey = $.trim($("#user-key").val());
		var taskId = $.trim($("#task-id").val());
		var reportCate = $("#report-sel-cate").val();
		
		reportCate = reportCate > 0 ? reportCate : null;
		
		var param = {
			state: state,
			userKey: userKey,
			taskId: taskId,
			reportCate: reportCate,
			pageNo: pageNo,
			pageCount: 10,
		};
		AQ.callAQJSON("getReportData.do", param, function(json) {
			var result = json.data;
			var data = [];
			for (var i = 0; i < result.length; ++i) {
				if(result[i].state == 2) 		result[i].state = "已处理：审核通过";
				else if(result[i].state == 3) 	result[i].state = "已处理：审核未通过";
				else 							result[i].state = "待处理";
				
				if(result[i].report_cate == 110101)			result[i].report_cate = "不改价";
				else if(result[i].report_cate == 110201)	result[i].report_cate = "虚假付款";
				else if(result[i].report_cate == 110202)	result[i].report_cate = "不按要求处理任务";
				else if(result[i].report_cate == 110203)	result[i].report_cate = "使用“淘宝客”等付款";
				else if(result[i].report_cate == 110301)	result[i].report_cate = "恶意诈骗";
				else if(result[i].report_cate == 110401)	result[i].report_cate = "其它";
				else if(result[i].report_cate == 110501)	result[i].report_cate = "协商取消任务";
				else 									result[i].report_cate = "未知";
				
				data.push(result[i]);
			}
			$scope.$apply(function () {
				$scope.listData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					var num_entries = 100;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						getData(page_index+1);
					});
				}
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	$("#query-data").click(function() {
        getData(1);
    });
	
	$scope.readContent = function(content) {
		$("#read-dialog").each(function(index, element) {
			$(element).remove();
		});
		var html = '<div id="read-dialog" style="width:850px; height:500px; overflow:auto;">'
			+ content +'</div>';
		var d = dialog({
			fixed: true,
			zIndex: 999999999,
			title: '投诉举报',
			content: html,
			okValue: '返回',
			ok: function () {}
		});
		d.width(850);
		d.height(500);
		d.showModal();
	};
	
	$scope.handleReport = function(id, taskId, reportUserId, reportedUserId) {
		info = "投诉举报"+ id +"；任务编号"+ taskId +"；";
		
		$("#handle-dialog").each(function(index, element) {
			$(element).remove();
		});
		var html = '<div id="handle-dialog" style="width:700px; height:500px; overflow:auto;">'+
		'系统恢复任务&nbsp;&nbsp;&nbsp;<input type="button" value="恢复任务" onClick="recoverTask(this, '+id+')">'+	
		'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
		'系统取消任务&nbsp;&nbsp;&nbsp;<input type="button" value="取消任务" onClick="cancelTask(this, '+taskId+')"><BR><BR>'+
			'取消任务：给发布方返佣金&nbsp;&nbsp;&nbsp;<input type="button" value="返佣金" id="back-feibi" disabled="true" onClick="backTaskMoney(this, '+id+', 2)"><BR><BR>'+
			'取消任务：给发布方返本金&nbsp;&nbsp;&nbsp;<input type="button" value="返本金" id="back-money" disabled="true" onClick="backTaskMoney(this, '+id+', 1)"><BR><BR>'+			
			'理赔：被举报方给举报方支付&nbsp;<input type="text" id="pay-fb-amount" style="width:50px">&nbsp;飞币&nbsp;&nbsp;&nbsp;<input type="button" value="理赔飞币" onClick="payFor(this, '+reportUserId+', '+reportedUserId+', 2)"><BR><BR>'+
			'理赔：被举报方给举报方支付&nbsp;<input type="text" id="pay-money-amount" style="width:50px">&nbsp;元&nbsp;&nbsp;&nbsp;<input type="button" value="理赔余额" onClick="payFor(this, '+reportUserId+', '+reportedUserId+', 1)"><BR><BR>'+
			'处罚被举报方：扣飞币&nbsp;<input type="text" id="punish-fb-amount" style="width:50px">&nbsp;飞币&nbsp;&nbsp;&nbsp;<input type="button" value="扣飞币" onClick="punishReported(this, '+reportedUserId+', 2)"><BR><BR>'+
			'处罚被举报方：扣余额&nbsp;<input type="text" id="punish-ye-amount" style="width:50px">&nbsp;元&nbsp;&nbsp;&nbsp;<input type="button" value="扣余额" onClick="punishReported(this, '+reportedUserId+', 1)"><BR><BR>'+			
			'处理结果：<BR><textarea type="text" id="handle-result" style="width:600px; height:80px;"></textarea>'+
			'<div style="padding-top:10px; text-align:center;">'+
			'<input type="button" value="审核未通过" class="check" disabled="true" onClick="submitHandle('+id+', 3)" style="width:120px; height:30px;  color:#FBEC0B; font-weight:bold;">'+
			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
			'<input type="button" value="审核通过" class="check" disabled="true" onClick="submitHandle('+id+', 2)" style="width:120px; height:30px; color:#449344; font-weight:bold;"></div>'+
			'</div>';
		var d = dialog({
			fixed: true,
			zIndex: 999999999,
			title: '处理投诉举报'+ id +'：[任务编号:'+ taskId +']',
			content: html,
			okValue: '返回',
			ok: function () {}
		});
		d.width(700);
		d.height(500);
		d.showModal();
	};
	

}]);

//理赔
function payFor(dom, benefitUserId, payUserId, punishType) {
	var oriText = $(dom).siblings("#handle-result").val();
	var value, newText;
	if(punishType == 1) {
		value = $(dom).siblings("#pay-money-amount").val();
		newText = "理赔：被举报方给举报方支付"+ value +"元；<BR>";
	}
	if(punishType == 2) {
		value = $(dom).siblings("#pay-fb-amount").val();
		newText = "理赔：被举报方给举报方支付"+ value +"飞币；<BR>";
	}
	
	var param = {
		benefitUserId: benefitUserId,
		payUserId: payUserId,
		punishType: punishType,
		value: value,
		info: info,
	};
	AQ.callAQJSON("payFor.do", param, function(json) {
		var info = json.info;
		Utils.msgDialog(info);
		$(dom).prop("disabled", true);
		$(dom).siblings("#handle-result").val(oriText + newText);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}

//取消任务
function cancelTask(dom, taskId) {
	var oriText = $(dom).siblings("#handle-result").val();
	var newText = "系统取消任务；<BR>";
	
	var param = {
		taskId: taskId,
		isCancel: 1,
	};
	AQ.callAQJSON("changeTaskIsCancel.do", param, function(json) {
		var info = json.info;
		Utils.msgDialog(info);
		$(dom).prop("disabled", true);
		$("#back-feibi").prop("disabled", false);
		$("#back-money").prop("disabled", false);
		$(".check").prop("disabled", false);
		$(dom).siblings("#handle-result").val(oriText + newText);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}

//恢复任务
function recoverTask(dom, id) {
	var oriText = $(dom).siblings("#handle-result").val();
	var newText = "系统恢复任务；<BR>";
	
	var param = {
		id: id,
	};
	AQ.callAQJSON("recoverReportedTask.do", param, function(json) {
		var info = json.info;
		Utils.msgDialog(info);
		$(dom).prop("disabled", true);
		$(".check").prop("disabled", false);
		$(dom).siblings("#handle-result").val(oriText + newText);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}

/**
 * 给发布方退还任务本金 或 佣金
 * 
 * @param dom
 * @param id
 * @param backType
 */
function backTaskMoney(dom, id, backType) {
	var oriText = $(dom).siblings("#handle-result").val();
	var newText = "系统取消任务：给发布方退还本金；<BR>";
	if(backType == 2) {
		newText = "系统取消任务：给发布方退还佣金；<BR>";
	}
	
	var param = {
		id: id,
		backType: backType,
		info: info,
	};
	AQ.callAQJSON("backTaskMoney.do", param, function(json) {
		var info = json.info;
		Utils.msgDialog(info);
		$(dom).prop("disabled", true);
		$(dom).siblings("#handle-result").val(oriText + newText);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}

/**
 * 处罚用户
 * 
 * @param dom
 * @param reportedUserId
 * @param punishType
 */
function punishReported(dom, reportedUserId, punishType) {
	var oriText = $(dom).siblings("#handle-result").val();
	var value, newText;
	if(punishType == 1) {
		value = $(dom).siblings("#punish-ye-amount").val();
		newText = "处罚被举报方：扣除余额"+ value +"元；<BR>";
	}
	if(punishType == 2) {
		value = $(dom).siblings("#punish-fb-amount").val();
		newText = "处罚被举报方：扣除飞币"+ value +"飞币；<BR>";
	}
	
	var param = {
		userId: reportedUserId,
		punishType: punishType,
		value: value,
		info: info,
	};
	AQ.callAQJSON("punishUser.do", param, function(json) {
		var info = json.info;
		Utils.msgDialog(info);
		$(dom).prop("disabled", true);
		$(dom).siblings("#handle-result").val(oriText + newText);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}

//提交处理
function submitHandle(id, state) {
	var handleResult = $.trim($("#handle-result").val());
	if(state == 3) {
		handleResult = "投诉举报证据描述不充足，请上传详细证据重新提交投诉或联系客服咨询；<BR>" + handleResult.replace(/\n/g, "<BR>");
	}
	var param = {
		id: id,
		state: state,
		handleResult: handleResult,
	};
	AQ.callAQJSON("handleReport.do", param, function(json) {
		$("#ID_"+id).parent("ul").remove();
		$(".ui-dialog").each(function(index, element) {
			$(element).parent("div").remove();
		});
		$(".ui-popup-backdrop").each(function(index, element) {
			$(element).remove();
		});
		
		var info = json.info;
		Utils.msgDialog(info);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}